package com.weilan.blog.picture.controller;

import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.picture.service.FileService;
import com.weilan.blog.picture.service.PictureService;
import com.weilan.blog.picture.utils.QiniuUtil;
import com.weilan.blog.picture.vo.PictureVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description: 图片服务相关接口
 * 图片上传FileController
 * @author: 孙 峰
 * @time: 2020/10/20 10:55
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @Autowired
    private QiniuUtil qiniuUtil;

    //获取上传路径
    @Value(value = "${file.upload.path}")
    private String path;

    /**
     * 多文件上传
     * 上传图片接口   传入 userId sysUserId ,有那个传哪个，记录是谁传的,
     * projectName 传入的项目名称如 base 默认是base
     * sortName 传入的模块名， 如 admin，user ,等，不在数据库中记录的是不会上传的
     *
     * @return
     */

    @PostMapping("/pictures")
    public synchronized Object uploadPics(HttpServletRequest request, List<MultipartFile> filedatas) {

        // 获取七牛云配置文件
        Map<String, String> qiNiuConfig = qiniuUtil.getQiNiuConfig();
        return fileService.uploadImgs(path, request, filedatas, qiNiuConfig);
    }

    @GetMapping("/getPicture")
    public String getPicture(
            @ApiParam(name = "fileIds", value = "文件ids", required = false) @RequestParam(name = "fileIds", required = false) String fileIds,
            @ApiParam(name = "code", value = "切割符", required = false) @RequestParam(name = "code", required = false) String code) {
        return fileService.getPicture(fileIds, code);
    }
}
